﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web.UI;
using DataAccessObject;
using EntityObject;
using SharedLibraries;
using SharedLibraries.Constants;
using ThangNM_LIB.EO;
using ThangNM_LIB.FUNC;

namespace ThangNM
{
    public partial class Answer : System.Web.UI.Page
    {
        public ThangNM.PublicInterface.Home_New mtpHome;

        protected void Page_Load(object sender, EventArgs e)
        {
            mtpHome = Master as ThangNM.PublicInterface.Home_New;
            mtpHome.setClearMsg();
            QuestionAnswer_DetailUC1.Visible = true;
            try
            {
                if (!IsPostBack)
                {
                    QuestionAnswerEO objTemp = new QuestionAnswerEO();
                    objTemp = getTemp();
                    if (Request.QueryString[QueryString.question] != null)
                        BindData(false);
                    else
                        Page.Title = "Hỏi đáp - Gửi câu hỏi mới" + System.Configuration.ConfigurationManager.AppSettings["SLOGAN"];

                }
            }
            catch (Exception ex)
            {
                if (mtpHome != null)
                    mtpHome.setNotifications(ConstEx.FunctionSetMsg.ERROR, ex.Message, e.ToString());
            }
            QuestionAnswer_DetailUC1.ToolBarUC1.Visible = false;
        }

        protected void BindData(Boolean isCmt)
        {
            QuestionAnswerEO _QuestionAnswerEO = new QuestionAnswerEO();
            QuestionAnswerExEO _QuestionAnswerExEO = new QuestionAnswerExEO();
            _QuestionAnswerEO.PK_iQuestionID = Convert.ToInt32(Request.QueryString[QueryString.question]);
            DataSet ds = (new QuestionAnswerDAO()).SelectBy_PK_iQuestionID(_QuestionAnswerEO.PK_iQuestionID);
            if (DataTableEx.DataTable_OK(null, ds, 0) == false)
            {
                Commons.setRedirect(Page, null);
                return;
            }
            _QuestionAnswerEO = (new QuestionAnswerDAO()).QuestionAnswerDO(ds.Tables[0]);
            String urlSEO = FileNameByFunction.Hoi_Dap_ + RewriteUrl.ConvertToUnSign(_QuestionAnswerEO.sTitle) + "." + _QuestionAnswerEO.PK_iQuestionID + FileNameByFunction.HTML;
            if (!StringEx.Compare(Request.RawUrl.Substring(1), urlSEO))
            {
                Response.Status = "301 Moved Permanently";
                Response.AddHeader("Location", System.Configuration.ConfigurationManager.AppSettings["HOMEPAGE"] + urlSEO);
                return;
            }
            object temp = JsonEx.Json2Object(_QuestionAnswerEO.sQuestionAnswerEx, _QuestionAnswerExEO.GetType());
            if (temp != null)
                _QuestionAnswerExEO = (QuestionAnswerExEO)temp;
            hfPK_iQuestionID.Value = _QuestionAnswerEO.PK_iQuestionID.ToString();
            lblsTitle.Text = _QuestionAnswerEO.sTitle;
            lblsTitle1.Text = _QuestionAnswerEO.sTitle;
            lblsContent.Text = _QuestionAnswerEO.sContent;
            lbltDateTimeCreate.Text = _QuestionAnswerEO.tDateTimeCreate.ToString(FormatEx.Date_Time.Format_DateTime_WebView);
            hplFK_iAccountID.NavigateUrl = "mailto:" + _QuestionAnswerEO.sEmail;
            lblFK_iAccountID.Text = _QuestionAnswerEO.sName;
            lbliVisit.Text = _QuestionAnswerEO.iVisit.ToString();
            lbliLike.Text = _QuestionAnswerEO.iLike.ToString();
            SEOEO _SEOEO = new SEOEO();
            Commons.setSEO_ON_PAGE(Page, _SEOEO, this.GetType());


            if (DataTableEx.DataTable_OK(null, ds, 1))
            {
                ds.Tables[1].DefaultView.Sort = "PK_iQuestionID ASC";
                ds.Tables[1].Columns.Add(new DataColumn("sDateTimeCreate", Type.GetType("System.String")));
                ds.Tables[1].Columns.Add(new DataColumn("sWebsite", Type.GetType("System.String")));
                QuestionAnswerExEO _QuestionAnswerExEO_1;
                SortedList<String, String> lstEmail = new SortedList<string, string>();
                String urlQA = System.Configuration.ConfigurationManager.AppSettings["HOMEPAGE"] + Request.RawUrl.Substring(1);
                String urlCancelNotification = "";
                foreach (DataRow dr in ds.Tables[1].Rows)
                {
                    dr["sDateTimeCreate"] = Convert.ToDateTime(dr["tDateTimeCreate"]).ToString(FormatEx.Date_Time.Format_DateTime_WebView);
                    _QuestionAnswerExEO_1 = new QuestionAnswerExEO();
                    temp = JsonEx.Json2Object(StringEx.Trim(Convert.ToString(dr["sQuestionAnswerEx"])), _QuestionAnswerExEO_1.GetType());
                    if (temp != null)
                        _QuestionAnswerExEO_1 = (QuestionAnswerExEO)temp;
                    dr["sWebsite"] = _QuestionAnswerExEO_1.sWebsite;
                    if (Convert.ToBoolean(dr["bNotification"]) && isCmt == true)
                        if (!lstEmail.ContainsKey(Convert.ToString(dr["sEmail"])))
                        {
                            QuestionAnswerEO objTemp = new QuestionAnswerEO();
                            objTemp.PK_iQuestionID = Convert.ToInt32(hfPK_iQuestionID.Value);
                            objTemp.sEmail = Convert.ToString(dr["sEmail"]);
                            objTemp.iStatus = 3;
                            urlCancelNotification = System.Configuration.ConfigurationManager.AppSettings["HOMEPAGE"] + FileNameByFunction.Token + Security.Token.getToken(new TokenEO(ConstEx.Token_iType_C.UnNotificationQA, DateTime.Now.AddHours(3), objTemp));
                            CustomerCache.AddNewItemEmail(new EmailEO(Convert.ToString(dr["sEmail"]), Convert.ToString(dr["sName"]), Email_iType_C.Notification_Comment, Email_iStatus_C.WAIT, urlQA, urlCancelNotification));
                            CustomerCache.AddNewItemEmail(new EmailEO("", "", 1, 1, _s2: "Sam"));
                            lstEmail.Add(Convert.ToString(dr["sEmail"]), Convert.ToString(dr["sName"]));
                        }
                }
                rptComment.DataSource = ds.Tables[1];
                rptComment.DataBind();
                lbliComment.Text = ds.Tables[1].Rows.Count.ToString();
            }
        }

        protected QuestionAnswerEO getTemp()
        {
            QuestionAnswerEO objTemp = new QuestionAnswerEO();
            if (Request.Cookies["autofill"] != null)
            {
                objTemp = (QuestionAnswerEO)JsonEx.Json2Object(Security.Login.Decrypt(Request.Cookies["autofill"].Value, System.Configuration.ConfigurationManager.AppSettings["FACEBOOK"]), objTemp.GetType());
                objTemp.FK_iAccountID = 0;
                objTemp.FK_iAnswerID = 0;
                objTemp.iLike = 0;
                objTemp.iVisit = 0;
                objTemp.PK_iQuestionID = 0;
                objTemp.sTitle = "";
                objTemp.tDateTimeCreate = DateTime.MinValue;
                objTemp.sContent = "";
            }
            return objTemp;
        }

        #region "Raise Event"
        protected void ViewMessageDetail_Click(object sender, CustomerEventArgs e)
        {
            if (e.iType == ConstEx.FunctionSetMsg.SUCCESS)
            {
                if (Request.QueryString[QueryString.question] != null)
                {
                    BindData(true);
                    QuestionAnswer_DetailUC1.txtsContent.Text = "";
                }
                else
                    Response.Redirect(System.Configuration.ConfigurationManager.AppSettings["HOMEPAGE"] + FileNameByFunction.Hoi_Dap);
                if (mtpHome != null)
                    mtpHome.setNotifications(e.iType, e.sMessage);
            }
        }
        #endregion

        protected void lbtniLike_Click(object sender, EventArgs e)
        {
            try
            {
                lbliLike.Text = (Convert.ToInt32(lbliLike.Text) + 1).ToString();
                QuestionAnswerEO _QuestionAnswerEO = new QuestionAnswerEO();
                _QuestionAnswerEO.PK_iQuestionID = Convert.ToInt32(hfPK_iQuestionID.Value); ;
                _QuestionAnswerEO.iLike = Convert.ToInt32(lbliLike.Text);
                (new QuestionAnswerDAO()).Update_bNotification_Or_iLike(_QuestionAnswerEO);
            }
            catch (Exception ex)
            {
                if (mtpHome != null)
                    mtpHome.setNotifications(ConstEx.FunctionSetMsg.ERROR, ex.Message, e.ToString());
            }
        }
    }
}